package com.sense.kukoo.biz_library.adapter.provider

import com.alibaba.android.arouter.utils.TextUtils
import com.google.gson.Gson
import com.sense.kukoo.biz_library.R
import com.sense.kukoo.biz_library.adapter.HistoryAdapter
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.HistoryItemBean
import com.sense.kukoo.common.repo.bean.LibraryListenBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.HistoryItemView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_logger.LoggerManager

class HistoryItemProvider(val mListener: HistoryAdapter.HistoryEventListener) :
    MultiItemProvider<HistoryItemBean>() {

    private val TAG: String = javaClass.simpleName

    var isEditMode:Boolean = false

    override fun getItemType(): Int = HistoryItemBean.TYPE_DEF

    override fun getItemLayoutId(): Int = com.sense.kukoo.common.R.layout.common_history_item_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, itemBean: HistoryItemBean) {
        val item: LibraryListenBean = itemBean.item

        val itemView = viewHolder.getView(com.sense.kukoo.common.R.id.item) as HistoryItemView

        //
        itemView.setItemClickListener {
            if (!isEditMode) {
                mListener.onItemClick(itemView, itemBean)
            }else{
                itemView.toggle()
            }
        }
        itemView.setItemLongClickListener {
            if (isEditMode){
                return@setItemLongClickListener false
            }else{
                mListener.onItemLongClick(itemView, itemBean)
                return@setItemLongClickListener true
            }
        }
        itemView.setOnCheckedChangedListener {
            val checked:Boolean = itemView.isChecked
            itemBean.isChecked = checked
            mListener.onItemCheckedChanged(itemView, itemBean)
        }
        //
        itemView.setEditMode(isEditMode)
        itemView.isChecked = itemBean.isChecked
        //
        val url = if(TextUtils.isEmpty(item.bookItem.coverUrl)) item.book.coverUrl else item.bookItem.coverUrl
        itemView.setIconUrl(url)
        //
        val name = if (TextUtils.isEmpty(item.bookItem.name)) item.book.name else item.bookItem.name
        itemView.setDes(name)

        if (item.book.type==BookBean.TYPE_AUDIOBOOK) {
            //显示最近听的
            itemView.setMsg(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.library_recently_hint))
            itemView.setMsg2(item.bookItem.name)
        }else{
            //显示剩余时长
            var len:Int = (item.bookItem.duration ?: 0) - (item.bookItem.listenTotal ?: 0)
            len = len/60
            len = if(len <=0 ) 1 else len
            //
            itemView.setMsg(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.library_min_left, len))
            itemView.setMsg2("")
        }
    }

}